/**
 * May 19, 2014 6:59:01 PM
 */
package elitech.vietnam.copyweatherfashion.dialog;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import elitech.vietnam.copyweatherfashion.R;

/**
 * @author Cong
 */
public class GetImageDialogFragment extends DialogFragment implements View.OnClickListener {

	private static final String ARG_LISTENER_TYPE = "ARGS_LISTENER_TYPE";
	
	GetImageChoosen	mListener;

	ImageButton		mBtnCamera, mBtnLibrary;
	
	public static enum ListenerType {
		ACTIVITY,
		FRAGMENT
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ListenerType type = (ListenerType) getArguments().getSerializable(ARG_LISTENER_TYPE);
		switch (type) {
		case ACTIVITY:
			mListener = (GetImageChoosen) getActivity();
			break;
		case FRAGMENT:
			mListener = (GetImageChoosen) getTargetFragment();
			break;
		default:
			break;
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.dialog_takeimage, container, false);

		mBtnCamera = (ImageButton) view.findViewById(R.id.takepic_btnCamera);
		mBtnLibrary = (ImageButton) view.findViewById(R.id.takepic_btnLibrary);

		mBtnCamera.setOnClickListener(this);
		mBtnLibrary.setOnClickListener(this);

		return view;
	}

	/**
	 * @deprecated: should use {@link #show(FragmentManager)} method instead
	 */
	@Deprecated
	@Override
	public void show(FragmentManager manager, String tag) {
		super.show(manager, tag);
	}
	
	public void show(FragmentManager manager) {
		super.show(manager, getClass().getName());
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.takepic_btnCamera:
			if (mListener != null)
				mListener.takeByCamera();
			break;
		case R.id.takepic_btnLibrary:
			if (mListener != null)
				mListener.takeByLibrary();
			break;
		default:
			break;
		}
		dismiss();
	}

	public interface GetImageChoosen {
		public void takeByCamera();

		public void takeByLibrary();
	}

	public static class Builder {
		GetImageChoosen	mListener;
		ListenerType mType;

		public Builder setCallback(GetImageChoosen listener, ListenerType type) {
			mListener = listener;
			mType = type;
			return this;
		}

		public GetImageDialogFragment build() {
			GetImageDialogFragment fragment = new GetImageDialogFragment();
			Bundle args = new Bundle();
	        args.putSerializable(ARG_LISTENER_TYPE, mType);
	        fragment.setArguments(args);
	        if (mListener instanceof Fragment)
	        	fragment.setTargetFragment((Fragment) mListener, 69);
			fragment.setStyle(STYLE_NO_TITLE, R.style.Dialog);
			return fragment;
		}
	}
}
